<!-- 排班管理 -->
<template>
  <div
    class="dashboard-container transport-task customer-list-box workArrange-manage-people-setting"
  >
    <div
      class="app-container"
      style="width: 100%"
    >
      <el-card
        class="search-card-box"
        shadow="never"
      >
        <el-form
          ref="searchfrom"
          :model="searchfrom"
          label-width="80px"
        >
          <el-row :gutter="60">
            <el-col :span="8">
              <el-form-item label="员工账号:">
                <el-input
                  v-model="searchfrom.employeeNumber"
                  clearable
                  placeholder="请输入员工账号"
                />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="员工姓名:">
                <el-input
                  v-model="searchfrom.name"
                  clearable
                  placeholder="请输入员工姓名"
                />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item
                label="工作模式:"
                style="margin-bottom: 0px"
                label-width="80px"
              >
                <el-select
                  v-model="searchfrom.workPatternId"
                  collapse-tags
                  placeholder="请选择工作模式"
                >
                  <el-option
                    v-for="item in options"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="60">
            <el-col :span="8">
              <el-form-item
                type="month"
                label="月份:"
                label-width="80px"
              >
                <el-date-picker
                  v-model="searchfrom.month"
                  type="month"
                  placeholder="请选择月份"
                />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item
                prop="username"
                label="所属机构:"
              >
                <treeselect
                  v-model="searchfrom.agencyId"
                  :options="agencyOptions"
                  :normalizer="normalizer"
                  :searchable="true"
                  style="width: 100%"
                  placeholder="请选择所属机构"
                  no-options-text="暂无数据"
                  no-results-text="暂无数据"
                  @select="selectAgency"
                  @open="open"
                  @close="close"
                />
                <img
                  ref="arrow"
                  src="../../assets/pullDown.png"
                  style="position:absolute;width:18px;height:18px;right:9px;top:12px;cursor:pointer;pointer-events:none;transition-duration:0.3s"
                />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-button
                type="warning"
                @click="handleFilter('查询')"
              >搜索</el-button>
              <el-button
                plain
                class="reset-btn"
                @click="resetForm('searchfrom')"
              >重置</el-button>
            </el-col>
          </el-row>
        </el-form>
      </el-card>

      <!-- 加载 -->
      <div
        v-loading="listLoading"
        :class="{ 'loading-box': listLoading }"
        element-loading-text="加载中"
      >

        <el-card
          slot="empty"
          class="table-empty-box"
          shadow="never"
          style="margin-top: 20px"
        >

          <el-button
            type="danger"
            style="margin-bottom: 20px;"
            @click="set"
          >排班设置</el-button>
          <el-button
            type="danger"
            plain
            style="margin-bottom: 20px;margin-left: 12px;"
            @click="change"
          >绑定排班</el-button>
          <el-table
            :data="datalist"
            @selection-change="selectchange"
          >
            <el-table-column
              type="selection"
              width="55"
            />
            <!-- 员工账号 -->
            <el-table-column
              fixed="left"
              align="left"
              prop="employeeNumber"
              label="员工账号"
              min-width="80px"
            >
            </el-table-column>
            <!-- 员工姓名 -->
            <el-table-column
              fixed="left"
              align="left"
              prop="name"
              label="员工姓名"
              min-width="100px"
            >
            </el-table-column>
            <!-- 员工电话 -->
            <el-table-column
              fixed="left"
              align="left"
              prop="phone"
              label="员工电话"
              min-width="120px"
            >
            </el-table-column>
            <!-- 员工角色 -->
            <el-table-column
              fixed="left"
              align="left"
              prop="userType"
              label="员工角色"
              min-width="100px"
            >
              <template v-slot="{ row }">
                <span v-if="row.userType === 1">员工</span>
                <span v-else-if="row.userType === 2">快递员</span>
                <span v-else>司机</span>
              </template>
            </el-table-column>
            <!-- 当前工作模式 -->
            <el-table-column
              fixed="left"
              align="left"
              prop="workPatternName"
              label="当前工作模式"
              min-width="120px"
            >
            </el-table-column>
            <!-- 操作按钮 -->
            <el-table-column
              v-for="(item, index) in work"
              :key="index"
              class-name="date"
              :label="String(Number(index) + 1)"
              width="80px"
            >
              <template v-slot="{ row }">
                <span
                  v-if="!row.workPatternName"
                  class="rest"
                >无</span>
                <span
                  v-if="
                    row.workPatternName &&
                      row.workSchedules[Number(index)] === false
                  "
                  class="rest"
                  style=" width: 24px;
                  height: 24px;
                  border-radius: 50%;
                  text-align: center;
                  font-size: 13px;
                  background: #eff3f8;
                  border: 1px solid #d8dde3;
                  color: #20232a;"
                >休</span>
                <span
                  v-if="
                    row.workPatternName &&
                      row.workSchedules[Number(index)] === true
                  "
                  class="work"
                  style="width: 24px;
                      height: 24px;
                      border-radius: 50%;
                      text-align: center;
                      font-size: 13px;
                      background: #ffeeeb;
                      border: 1px solid #e15536;
                       color: #e15536;"
                >上</span>
              </template>
            </el-table-column>
            <!-- 空状态 -->
            <div
              v-show="(!datalist || datalist.length <= 0) && !listLoading"
              slot="empty"
            >
              <img
                src="@/icons/pic-kong@2x.png"
                alt="img"
                style="margin-top: 20px;width: 25%;height: 25%; margin-left: -90%;"
              />
              <p
                style="
           margin-top: -20px;
           padding-bottom: 0px;
           margin-left: -90%;
           "
              >
                这里空空如也
              </p>
            </div>
          </el-table>
          <!-- end -->
          <!-- 分页 -->
          <div class="pagination">
            <el-pagination
              :current-page="Number(searchfrom.page)"
              :total="Number(total)"
              :page-size="Number(searchfrom.pageSize)"
              :page-sizes="[10, 20, 30, 50]"
              layout="total, sizes, prev, pager, next, jumper"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
            />
          </div>
        </el-card>
        <!-- 绑定排班弹框 -->
        <el-dialog
          title="人工调整"
          :visible.sync="centerDialogVisible"
          width="35%"
          class="work-type"
          @closed="closing"
        >
          <el-form
            ref="changeForm"
            style="margin-left: 10%;"
            :model="changeForm"
          >
            <el-form-item
              label=""
              style="margin-bottom: 0px"
              prop="workPatternType"
            >
              <el-radio-group v-model="changeForm.workPatternType">
                <el-radio label="1">礼拜制</el-radio>
                <el-radio
                  label="2"
                  style="margin-left: 0%;"
                >连续制</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item
              label="模式名称："
              style="width: 70%"
            >
              <el-select
                v-model="changeForm.workPatternId"
                style="margin-left: 20px;"
                placeholder="请选择工作模式"
              >
                <el-option
                  v-for="item in options"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-form>
          <span
            slot="footer"
            class="dialog-footer"
          >
            <el-button
              type="danger"
              class="el-button"
              @click="submit"
            >确 定</el-button>
            <el-button
              class="el-button"
              style="margin-right:30%"
              @click="centerDialogVisible = false"
            >取 消</el-button>
          </span>
        </el-dialog>
      </div>
    </div>
  </div>
</template>
<script>
import { agencyList } from '@/api/institutions'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import { workManage, selectWorkHistoryList, peopleSet } from '@/api/transit'
export default {
  components: {
    Treeselect
  },
  data() {
    return {
      listLoading: false, // 加载
      searchfrom: {
        employeeNumber: null,
        month: '',
        name: '',
        workPatternId: null,
        agencyId: null,
        page: 1,
        pageSize: 10,
        userType: ''
      },
      normalizer(node) {
        return {
          id: node.id,
          label: node.name,
          children: node.children
        }
      },
      options: [],
      agencyOptions: [],
      datalist: [],
      total: 0,
      work: [],
      centerDialogVisible: false,
      changeForm: {
        workPatternType: '',
        workPatternId: null,
        userIdList: null,
        userTypes: ''
      }
    }
  },
  created() {
    this.getworkList()
    this.getAgencyList()
    this.getlist()
  },
  methods: {
    async getlist() {
      this.listLoading = true
      const res = await workManage(this.searchfrom)
      this.listLoading = false
      this.datalist = res.data.items
      this.total = res.data.counts
      // this.work = res.data.items[0].workSchedules
      if (this.datalist.length !== 0) {
        this.work = res.data.items[0].workSchedules
      }
    },
    async getAgencyList() {
      const res = await agencyList()
      this.agencyOptions = JSON.parse(res.data)
    },
    async getworkList() {
      const res = await selectWorkHistoryList()
      this.options = res.data[1]
    },
    handleFilter() {
      this.getlist()
    },
    resetForm() {
      this.$refs.searchfrom.resetFields()
      this.searchfrom = {
        employeeNumber: null,
        month: '',
        name: '',
        workPatternId: null,
        agencyId: null,
        page: 1,
        pageSize: 10,
        userType: ''
      }
      this.getlist()
    },
    selectAgency(val) {
      this.resetForm.agencyId = val.id
    },
    // 打开所属机构下拉框
    open() {
      this.$refs.arrow.style.transform = 'rotate(-180deg)'
    },
    // 关闭所属机构下拉框
    close() {
      this.$refs.arrow.style.transform = 'rotate(0deg)'
    },
    handleSizeChange(value) {
      this.searchfrom.pageSize = value
      this.getlist()
    },
    handleCurrentChange(val) {
      this.searchfrom.page = val
      this.getlist()
    },
    set() {
      this.$router.push('workArrange-setting')
    },
    change() {
      if (!this.changeForm.userIdList) {
        this.$message.error('请选择调整人员')
      } else {
        this.centerDialogVisible = true
      }
    },
    selectchange(selection) {
      this.changeForm.userIdList = selection.map(items => items.id)
      console.log(this.changeForm.userIdList)
      // this.changeForm.userTypes = selection.map(items => items.userType)
      console.log(this.changeForm.userTypes)
    },
    async submit() {
      await peopleSet(this.changeForm)
      this.getlist()
      this.centerDialogVisible = false
      this.$message.success('操作成功')
    },
    closing() {
      this.changeForm = {
        workPatternType: '',
        workPatternId: null,
        userIdList: [],
        userTypes: ''
      }
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
::v-deep th.date {
  .cell {
    padding-left: 17px !important;
    height: 25px;
  }
}
::v-deep .work {
  display: inline-block;
  width: 24px !important;
  height: 24px !important;
  background: #ffeeeb;
  border: 1px solid #e15536;
  border-radius: 50%;
  text-align: center;
  color: #e15536;
  font-size: 13px;
}
.history-name {
  width: auto !important;
  text-align: center;
  ::v-deep .el-input__inner {
    width: 293px;
  }
}

.workArrange-manage-people-setting {
  ::v-deep .el-dialog__body {
    padding: 30px 50px 10px 50px !important;
  }
  ::v-deep .el-dialog__footer {
    padding-bottom: 40px;
  }
}

.work-type {
  ::v-deep .el-form-item__content {
    margin-left: 40px !important;
  }
  ::v-deep .el-radio:first-child {
    margin-right: 80px !important;
  }
  ::v-deep.el-radio__label {
    font-weight: bold;
    color: #333333;
  }
}

.dialog-footer {
  .el-button {
    width: 89px;
    height: 36px;
    padding: 10px 20px;
  }
}

::v-deep .rest {
  display: inline-block;
  width: 24px;
  height: 24px;
  background: #eff3f8;
  border: 1px solid #d8dde3;
  border-radius: 50%;
  text-align: center;
  color: #20232a;
  font-size: 13px;
}

.alert {
  margin: 10px 0px;
}
::v-deep .pagination {
  // margin-top: 40px;
  margin: 20px 0 0 !important;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px !important;
  color: #20232a !important;
}
.transport-task ::v-deep .el-table td,
.el-table th {
  padding: 12px 0;
  min-width: 0;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  text-overflow: ellipsis;
  vertical-align: middle;
  position: relative;
  text-align: left;
  overflow: hidden;
}

// 下拉框
::v-deep .el-select {
  width: 100%;
}

/*定义滚动条轨道 内阴影+圆角*/
::v-deep .el-table--scrollable-x .el-table__body-wrapper {
  padding-bottom: 5px;
  margin-bottom: 5px;
  &::-webkit-scrollbar {
    height: 10px;
  }

  /*定义滑块 内阴影+圆角*/
  &::-webkit-scrollbar-thumb {
    border-radius: 10px;
    background-color: rgba(144, 147, 153, 0.3);
  }

  &::-webkit-scrollbar-track-piece {
    margin-right: 3px;
    margin-left: 3px;
  }
}
::v-deep .el-card {
  overflow: initial;
}

::v-deep .el-header {
  background-color: #b3c0d1;
  color: #333;
  text-align: center;
  line-height: 60px;
}
.el-main {
  background-color: #e9eef3;
  color: #333;
  text-align: center;
  line-height: 160px;
}
</style>

<style></style>
